package com.lizhi.yun.core.user.handler;/**
 * @program: spring-boot
 * @name UppUserHandler
 * @description:
 * @author: lizhi
 * @create: 2020-04-13 12:36
 */

import com.lizhi.common.yun.entity.ListResponseResult;
import com.lizhi.common.yun.enums.RoleTypeEnum;
import com.lizhi.common.yun.enums.UserStateEnum;
import com.lizhi.yun.base.common.Constants;
import com.lizhi.yun.db.dao.UppOrganDao;
import com.lizhi.yun.db.dao.UppRoleDao;
import com.lizhi.yun.db.dao.UppUserDao;
import com.lizhi.yun.db.pojo.UppOrganPOJO;
import com.lizhi.yun.db.pojo.UppRolePOJO;
import com.lizhi.yun.db.pojo.UppUserPOJO;
import com.lizhi.mybatis.interceptor.PageBounds;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.date.DateUtils;
import com.lizhi.utils.tools.MD5Util;
import com.lizhi.yun.base.BaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *@program: spring-boot
 *@name UppUserHandler
 *@description:
 *@author: lizhi
 *@create: 2020-04-13 12:36
 */
@Slf4j
@Service
public class UppUserHandler extends BaseHandler {

    @Resource
    private UppUserDao uppUserDao;

    @Resource
    private UppRoleDao uppRoleDao;

    @Resource
    private UppOrganDao uppOrganDao;

    public ListResponseResult doMain(Map<String, Object> map) {
        /*判断所属医院*/
        if (map.containsKey("organIds")){
            String organId = map.get("organIds").toString();
            if (Constants.ORGAN_ID.equals(organId)){
                map.put("organIds", null);
            }
        }
        PageBounds pageBounds = getPageBounds(map);
        List<UppUserPOJO> listUppUserPOJO = uppUserDao.selectByMap(map, pageBounds);
        for (UppUserPOJO uppUserPOJO : listUppUserPOJO){
            /*获取角色名串*/
            String roleIds = uppUserPOJO.getRoleIds();
            String[] roleId = roleIds.split(",");
            StringBuffer roleIdsBuffer = new StringBuffer();
            for (int i = 0; i < roleId.length; i ++){
                UppRolePOJO uppRolePOJO = uppRoleDao.selectById(roleId[i]);
                if (i == roleId.length -1){
                    roleIdsBuffer.append(uppRolePOJO.getRoleNm());
                } else {
                    roleIdsBuffer.append(uppRolePOJO.getRoleNm() + ",");
                }
            }
            uppUserPOJO.setRoleNms(roleIdsBuffer.toString());

            /*获取医院名串*/
            String organIds = uppUserPOJO.getOrganIds();
            String[] organId = organIds.split(",");
            StringBuffer organIdsBuffer = new StringBuffer();
            for (int i = 0; i < organId.length; i ++){
                UppOrganPOJO uppOrganPOJO = uppOrganDao.selectById(organId[i]);
                if (i == organId.length -1){
                    organIdsBuffer.append(uppOrganPOJO.getOrganNm());
                } else {
                    organIdsBuffer.append(uppOrganPOJO.getOrganNm() + ",");
                }
            }
            uppUserPOJO.setOrganNames(organIdsBuffer.toString());
        }
        return getListResult(pageBounds.getCountRows(), listUppUserPOJO, map);
    }

    /**
     *功能描述 新增用户
     * @author lizhi
     * @date 2020/4/14
     * @param uppUserPOJO:
     * @return int
     */
    public ListResponseResult doAdd(UppUserPOJO uppUserPOJO){
        uppUserPOJO.setUserId(StringUtils.newId());
        uppUserPOJO.setIstTm(DateUtils.getNowDate());
        uppUserPOJO.setUserPwd(MD5Util.getMD5("123456"));
        //是否需要修改密码 0不用修改；1需要修改密码
        uppUserPOJO.setPsdChgFlg("1");
        /*设置用户状态正常*/
        uppUserPOJO.setState(UserStateEnum.NORMAL.getEnValue());
        return getListResult(uppUserDao.insertRecord(uppUserPOJO));
    }

    /**
     *功能描述  修改用户信息
     * @author lizhi
     * @date 2020/4/14
     * @param uppUserPOJO:
     * @return int
     */
    public ListResponseResult doUpdate(UppUserPOJO uppUserPOJO){
//        log.info("获取到要更新的信息是uppUserPOJO:{}", uppUserPOJO);
        uppUserPOJO.setUpdTm(DateUtils.getNowDate());
        if (StringUtils.isEmpty(uppUserPOJO.getUserPwd())){
            uppUserPOJO.setUserPwd(null);
        }
        return getListResult(uppUserDao.updateByIdSelective(uppUserPOJO));
    }

    /**
     *功能描述  删除用户信息
     * @author lizhi
     * @date 2020/4/14
     * @param userAcct:
     * @return int
     */
    public ListResponseResult doDelete(String userAcct){
        List<String> listStr = Arrays.asList(userAcct.split(","));
        return getListResult(uppUserDao.deleteByList(listStr));
    }

    /**
     *功能描述  判断用户是否存在
     * @author lizhi
     * @date 2020/4/14
     * @param userAcct:
     * @return boolean
     */
    public ListResponseResult isNotExists(String userAcct) {
        UppUserPOJO uppUserPOJO = uppUserDao.selectById(userAcct);
        if (null == uppUserPOJO) {
            return getListResult(true);
        }
        return getListResult(false);
    }

    /**
     *功能描述  查询出所有的机构
     * @author lizhi
     * @date 2020/4/14
     * @param userAcct:
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public ListResponseResult findById(String userAcct) {
        UppUserPOJO uppUserPOJO = uppUserDao.selectById(userAcct);
        return getListResult(uppUserPOJO);
    }

    /**
     * 判断用户密码是否正确
     * @param userPwd
     * @param userAcct
     * @return
     */
    public ListResponseResult checkPwd(String userPwd, String userAcct){
        UppUserPOJO uppUserPOJO = uppUserDao.selectById(userAcct);
        if (MD5Util.getMD5(userPwd).equals(uppUserPOJO.getUserPwd())) {
            return getListResult(true);
        }
        return getListResult(false);
    }
    /**
     *功能描述  更新用户账号密码
     * @author lizhi
     * @date 2020/4/14
     * @param oldPassword:
     * @param newPassword:
     * @param userAcct:
     * @return java.lang.String
     */
    public ListResponseResult updatePwd(String oldPassword, String newPassword, String userAcct) {
        UppUserPOJO uppUserPOJO = uppUserDao.selectById(userAcct);
        if (!MD5Util.getMD5(oldPassword).equals(uppUserPOJO.getUserPwd())) {
            return getListResult(false);
        }
        uppUserPOJO.setUserPwd(MD5Util.getMD5(newPassword));
        uppUserPOJO.setPsdChgFlg("0");
        uppUserPOJO.setPwdEditTm(DateUtils.getNowDate());
        uppUserPOJO.setState(UserStateEnum.NORMAL.getEnValue());
        int i = uppUserDao.updateByIdSelective(uppUserPOJO);
        if (i < 0){
            return getListResult(false);
        }
        return getListResult(true);
    }

    /**
     *功能描述   重置密码
     * @author lizhi
     * @date 2020/4/14
     * @param userAcct:
     * @return java.lang.String
     */
    public ListResponseResult resetPwd(String userAcct) {
        String userPwd = MD5Util.getMD5("123456");
        int i = uppUserDao.resetPwdbyId(userPwd,userAcct);
        return getListResult(i);
    }

    /**
     * 获取全部的医生列表
     * @return
     */
    public ListResponseResult getPhysicians(){
        List<UppUserPOJO> list = uppUserDao.selectByRoleIds(RoleTypeEnum.PHYSICIAN.getEnValue());
        return getListResult(list);
    }
}
